package cn.okcmd.mymqconsumer.services;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;


/**
 * create by caoqg on 2021/8/4
 * 功能说明:
 */
@Slf4j
@Component
public class MqConsumer2 {


    @RabbitListener(queues = "sms.q.myqueue")
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws Exception {
        //	1. 收到消息以后进行业务端消费处理
        log.info("-----------------------");
        Object payload = message.getPayload();
        byte[] body = (byte[]) payload;
        String string = new String(body);
        log.info("consumer2 消费消息:" + string);

        //  2. 处理成功之后 获取deliveryTag 并进行手工的ACK操作, 因为我们配置文件里配置的是 手工签收
        //	spring.rabbitmq.listener.simple.acknowledge-mode=manual
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);
    }
}
